package com.paypalpay.util.wx;

import java.net.URISyntaxException;
import com.paypalpay.config.WxPayConstants;
import com.paypalpay.util.wx.entity.AccessToken;
import com.paypalpay.util.wx.entity.JsapiTicket;
import com.paypalpay.util.wx.entity.NetWorkHelper;
import com.paypalpay.util.wx.entity.WYSQAccessToken;
import net.sf.json.JSONObject;

public class AccessTokenUtil {

	private static final String APP_ID = WxPayConstants.APPID;
	private static final String APP_SECRET = WxPayConstants.APP_SECRET;
	//网页授权中redirect_uri是指服务器回调到我们自己的配置域名
	private static final String CODE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ AccessTokenUtil.APP_ID+ "&redirect_uri=http://www.baidu.com&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
    //全局access_token获得链接
	private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ AccessTokenUtil.APP_ID + "&secret=" + AccessTokenUtil.APP_SECRET;
   //网页授权access_token获得链接
	private static final String WYSQ_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ AccessTokenUtil.APP_ID+ "&secret="+ AccessTokenUtil.APP_SECRET+ "&code="+AccessTokenUtil.CODE+"&grant_type=authorization_code";

	private static AccessToken accessToken = null;
	private static WYSQAccessToken access_token = null;

	// 全局access_token
	public static AccessToken getAccessToken() {

		if (null != accessToken) {
			return accessToken;
		}
		String result ;
		try {
			result = HttpClientTool.httpGet(ACCESS_TOKEN_URL, null);

			JSONObject jsonObject = JSONObject.fromObject(result);
			accessToken = (AccessToken) JSONObject.toBean(jsonObject,AccessToken.class);
			return accessToken;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	// 通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token（该access_token用于调用其他接口）不同

	public static WYSQAccessToken getAccess_token() {
		if (null != access_token) {
			return access_token;
		}
		String result;
		try {
			result = HttpClientTool.httpGet(WYSQ_ACCESS_TOKEN_URL, null);
			JSONObject jsonObject = JSONObject.fromObject(result);
			access_token = (WYSQAccessToken) JSONObject.toBean(jsonObject,WYSQAccessToken.class);

			return access_token;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}
	
	
	public static JsapiTicket getJSApiTicket() {
		// 获取token
		String acess_token = JsapiTicketUtil.getAccessToken();
		NetWorkHelper netHelper = new NetWorkHelper();
		String Url = String.format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ acess_token + "&type=jsapi", accessToken);
		String result = netHelper.getHttpsResponse(Url, "");
		System.out.println(result);
		// response.getWriter().println(result);
		JSONObject json = JSONObject.fromObject(result);
		JsapiTicket jsapiTicket = new JsapiTicket();
		jsapiTicket.setJsapiTicket(json.getString("ticket"));
		jsapiTicket.setExpiresin(json.getInt("expiresin"));

		return jsapiTicket;
	}

}
